package it.rikesh.aip2011.server.service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;

import it.rikesh.aip2011.client.service.HutService;
import it.rikesh.aip2011.server.meta.HutMeta;
import it.rikesh.aip2011.shared.model.Activity;
import it.rikesh.aip2011.shared.model.Hut;
import it.rikesh.aip2011.shared.model.HutActivity;
import it.rikesh.aip2011.shared.model.HutTrail;
import it.rikesh.aip2011.shared.model.Trail;

/**
 * @author  RN
 */
public class HutServiceImpl implements HutService {
    private static final Logger log = Logger.getLogger(HutServiceImpl.class.getName());
    private HutMeta h = new HutMeta();
    
    public List<Hut> getHutList() {
        log.info("Query getHutList");
        return Datastore.query(h).asList();
    }
    
    public String getHutName(String id){
        Key key=Datastore.createKey(Hut.class, Long.parseLong(id));
        Hut hut=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        log.info("Query getHutName("+id+") eseguito");
        return hut.getName();
        
    }
    
    public String getHutTitle(String id){
        Key key=Datastore.createKey(Hut.class, Long.parseLong(id));
        Hut hut=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        log.info("Query getHutTitle("+id+") eseguito");
        return hut.getTitle();
        
    }
    
    public String getHutPosition(String id){
        Key key=Datastore.createKey(Hut.class, Long.parseLong(id));
        Hut hut=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        log.info("Query getHutPosition("+id+") eseguito");
        return hut.getPosition();
        
    }
    
    public String getHutDDescription(String id){
        Key key=Datastore.createKey(Hut.class, Long.parseLong(id));
        Hut hut=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        log.info("Query getHutDDescription("+id+") eseguito");
        return hut.getDDescription().getValue();
        
    }
    
    public String getHutImg(String id){
        Key key=Datastore.createKey(Hut.class, Long.parseLong(id));
        Hut hut=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        log.info("Query getHutImg("+id+") eseguito");
        return hut.getImg();
        
    }
    
    public List<Activity> getHutActivity(String id){
        Key key=Datastore.createKey(Hut.class, Long.parseLong(id));
        Hut hut=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        List<Activity> list=new ArrayList<Activity>();
        for (HutActivity ha : hut.getHutActivityListRef().getModelList()) {
            list.add(ha.getActivityRef().getModel());
        }
        log.info("Query getHutActivity("+id+") eseguito");
        return list;
    }
    
    public List<Trail> getHutTrail(String id){
        Key key=Datastore.createKey(Hut.class, Long.parseLong(id));
        Hut hut=Datastore.query(h)
            .filter(h.key.equal(key))
            .asSingle();
        List<Trail> list=new ArrayList<Trail>();
        for (HutTrail ha : hut.getHutTrailListRef().getModelList()) {
            list.add(ha.getTrailRef().getModel());
        }
        log.info("Query getHutTrail("+id+") eseguito");
        return list;
    }
    
    public String getHutId(String name){
        Hut hut=Datastore.query(HutMeta.get()).filter(HutMeta.get().name.equal(name)).asSingle();
        log.info("Query getHutId("+name+") eseguito");
        return String.valueOf(hut.getKey().getId());
        
    }
}
